#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2015年6月19日

@author: chenyongbing
'''
import sys,os,tarfile
#current_dir = os.path.dirname(__file__)
current_dir = os.path.split(os.path.realpath(__file__))[0]

import datetime,logging

sys.path.append('../')
from base.time_method import myTimeMethod
from datatuning_datas import myDataTuningDatas



'''
create table flow_with_device_hour_rssi (day date,device varchar(17),hour int(2),rssi int(3),count int)

'''

class StatDeviceHourFlow():
    def __init__(self,st=None,et=None):
        if st==None or et==None:
            self.st = (datetime.date.today() - datetime.timedelta(1)).strftime('%Y-%m-%d')
            self.et = self.st
        else:
            self.st = st
            self.et = et
            
    def openTarFile(self,rawfile_tar):
        output = ''
        if not os.path.exists(rawfile_tar):
            logging.error('file %s not exists.'%rawfile_tar)
            return output
        #tar = tarfile.open(rawfile_tar)
        try:
            tar = tarfile.open(rawfile_tar)
            
        except :
            logging.error('tar file error.')
            return output
        for member in tar.getmembers():
            f = tar.extractfile(member)
            out = f.read()
            output = output + out+'\n'
        tar.close()
        return output
    
    def openTarFileEveryFile(self,rawfile_tar):
        output = ''
        if not os.path.exists(rawfile_tar):
            logging.error('file %s not exists.'%rawfile_tar)
            yield '',output
        else:
            try:
                tar = tarfile.open(rawfile_tar)
                for member in tar.getmembers():
                    f = tar.extractfile(member)
                    
                    out = f.read()
                    yield member.name,out
                tar.close()
            except :
                logging.error('tar file error.')
                yield '',output
        
    def dict_to_list(self,day,datas={}):
        datalist = []
        for dev , data in datas.items():
            #print dev
            for hour,hourdata in data.items():
                for rssi,flow in hourdata.items():
                    datalist.append([ day,dev,hour,rssi,flow,flow])
        return datalist
  
    def stat_device_hour_flow_by_rssi(self,rawpath = '/data/zmexport/rawdata/rawdata_group/',group=None):
        days = myTimeMethod.get_slist_between_st_et(self.st, self.et)
        
        for day in days:
            dayRawPath = rawpath+day
            #dayRawPath = rawpath
            for rname in os.listdir(dayRawPath):
                datas = {}
                if group!=None and rname != '%s.txt.tar.gz'%group :continue
                tarrawfile = dayRawPath + '/' + rname
                for device,out in self.openTarFileEveryFile(tarrawfile):
                    data = {}
                    for line in out.split('\n'):
                        lineList = line.split('.')
                        if len(lineList)!=6:continue
                        device = lineList[1]
                        hour = int(lineList[3].split(':')[0])
                        rssi = lineList[4]
                        if hour not in data.keys():
                            data[hour] = {}
                        if rssi not in data[hour].keys():
                            data[hour][rssi] = 0
                        data[hour][rssi]+=1
                    datas[device] = data
                
                datalist = self.dict_to_list(day, datas)
                myDataTuningDatas.insert_flow_with_device_hour_rssi( datas=datalist)
    
if __name__=='__main__':

    import argparse,re,datetime,time
    parser = argparse.ArgumentParser(description='args')
    parser.add_argument('--st',metavar=u"starttime",default=None)
    parser.add_argument('--et',metavar=u"endtime",default=None)
    parser.add_argument('--group',metavar=u"rawdata backup size",default=None)
    parser.add_argument('--rawpath',metavar='rawpath',default='/data/zmexport/rawdata/rawdata_group/')
    args = parser.parse_args()
    st = args.st
    et = args.et
    group = args.group
    rawpath = args.rawpath
    
    myStatDeviceHourFlow = StatDeviceHourFlow(st=st,et=et)
    
    
    myStatDeviceHourFlow.stat_device_hour_flow_by_rssi(rawpath=rawpath,group=group)
      

    

